﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MTool.Domain
{
    class JsonParser
    {
        public static string FormatJson(string jsonString)
        {
            // 去除空格和换行符
            jsonString = Regex.Replace(jsonString, @"\s+", "");

            // 添加引号
            //jsonString = AddQuotes(jsonString);

            // 格式化缩进
            jsonString = IndentJson(jsonString);

            // 移除多余的引号
            jsonString = RemoveQuotes(jsonString);

            return jsonString;
        }

        private static string AddQuotes(string jsonString)
        {
            jsonString = jsonString.Replace("{", "{\"");
            jsonString = jsonString.Replace(":", "\":\"");
            jsonString = jsonString.Replace(",", "\",\"");
            jsonString = jsonString.Replace("}", "\"}");

            return jsonString;
        }

        private static string IndentJson(string jsonString)
        {
            int indentLevel = 0;
            string indentString = "    ";
            string formattedJson = "";
            bool inString = false;

            foreach (char c in jsonString)
            {
                switch (c)
                {
                    case '{':
                    case '[':
                        formattedJson += c;
                        if (!inString)
                        {
                            formattedJson += "\n" + GetIndentString(indentLevel + 1, indentString);
                            indentLevel++;
                        }
                        break;

                    case '}':
                    case ']':
                        if (!inString)
                        {
                            indentLevel--;
                            formattedJson += "\n" + GetIndentString(indentLevel, indentString);
                        }
                        formattedJson += c;
                        break;

                    case ',':
                        formattedJson += c;
                        if (!inString)
                        {
                            formattedJson += "\n" + GetIndentString(indentLevel, indentString);
                        }
                        break;

                    case '"':
                        formattedJson += c;
                        inString = !inString;
                        break;

                    default:
                        formattedJson += c;
                        break;
                }
            }

            return formattedJson;
        }

        private static string GetIndentString(int indentLevel, string indentString)
        {
            return new string(indentString[0], indentLevel * indentString.Length);
        }

        private static string RemoveQuotes(string jsonString)
        {
            jsonString = jsonString.Replace("\"{", "{");
            jsonString = jsonString.Replace("}\"", "}");
            jsonString = jsonString.Replace("\":\"", ":");
            jsonString = jsonString.Replace("\",\"", ",");
            jsonString = jsonString.Replace("\"[", "[");
            jsonString = jsonString.Replace("]\"", "]");

            return jsonString;
        }
    }

    //class Program
    //{
    //    static void Main(string[] args)
    //    {
    //        string jsonString = "{name: John, age: 30, city: New York}";

    //        string formattedJson = JsonParser.FormatJson(jsonString);

    //        Console.WriteLine(formattedJson);
    //    }
    //}
}
